package Test10;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * Created by 14229 on 2020/2/21.
 */
public class Test {
    public static void main(String[] args) throws IOException {

        Person p = new Person(10000);

        Scanner sc = new Scanner(System.in);
        while (true) {
            if (p.getMoney() < 300) {
                System.out.println("余额不足 服务停止");
                return;
            }
            System.out.println("请输入租赁的类型: 1.轿车,2.客车");
            int type = sc.nextInt();
            switch (type) {
                case 1:
                    showCar(sc, p);
                    break;
                case 2:
                    showBus(sc, p);
                    break;
                case 0:
                    System.out.println("服务终止,  下次再来啊");
                    return;
                default:
                    System.out.println("没有该功能");
                    break;
            }
        }

    }


    public static  void saveRec(Reckon reckon) throws IOException {
        File f = new File("dir");
        f.mkdir();
        File f1 = new File("dir/history.txt");
        f1.createNewFile();

        List<String> lines = FileUtils.readLines(new File("dir/history.txt"), "UTF-8");
        ArrayList<Reckon> list = new ArrayList<>();

        for (String line : lines) {
            String[] ss = line.split(",");
            int i = Integer.parseInt(ss[4].replace("辆",""));
            list.add(new Car(ss[0],ss[1] ,ss[2], 0, Long.valueOf(ss[3]) , i, 0));
        }
        int flag = -1;
        for (Reckon re : list) {
            if(re.getPinpai().equals(reckon.getPinpai())){
                flag  = 1;
                re.setCount(re.getCount()+1);
                break;
            }
        }
        if(flag == -1){
            list.add(reckon);
        }
        //若干行代码
        FileUtils.writeLines(new File("dir/history.txt"), list);

    }

    public static void showBus(Scanner sc, Person p) throws IOException {
        System.out.println("客车相关的");
        System.out.println("请输入租赁的天数");
        int daysBus = sc.nextInt();
        System.out.println("请输入要称作的人数 16人以下 800/天, 16人(含)以上1500/天");

        int i = sc.nextInt();
        Bus bs = new Bus("大A888888", "大运", "彩色", i >= 16 ? 1500 : 800, new Date().getTime(), daysBus, i);
        p.setMoney(p.getMoney() - bs.money(bs));

        System.out.println("剩余的钱数: " + p.getMoney());
        bs.setCount(1);
        saveRec(bs);
    }

    public static void showCar(Scanner sc, Person p) throws IOException {
        System.out.println("请输入租赁的天数");
        int days = sc.nextInt();
        System.out.println("请输入要租赁的车型");
        System.out.println("    1.别克商务 600元/天");
        System.out.println("    2.宝马550i 500元/天");
        System.out.println("    3.别克林荫大道 300元/天");
        //若干行代码
        int chex = sc.nextInt();
        Car c = null;
        switch (chex) {
            case 1:
                c = new Car("别A888888", "别克商务", "金色", 600, new Date().getTime(), days);
                break;
            case 2:
                c = new Car("宝A888888", "宝马550i", "黑色", 500, new Date().getTime(), days);
                break;
            case 3:
                c = new Car("林A888888", "别克林荫大道", "白色", 300, new Date().getTime(), days);
                break;
        }
        p.setReckon(c);
        p.setMoney(p.getMoney() - c.money(c));
        System.out.println("剩余的钱数: " + p.getMoney());
        c.setCount(1);
        saveRec(c);
    }

}

